<script>
import {ref, reactive, watch} from 'vue'
import {orderStatus} from "@/api/constants";
import {findOrderList,confirmOrder,deleteOrder} from "@/api/order";
import OrderItem from "@/views/member/order/components/order-item.vue";
import XtxPagination from "@/components/library/XtxPagination.vue";
import OrderCancel from "@/views/member/order/components/order-cancel.vue";
import Confirm from "@/components/library/confirm";
import message from "@/components/library/message";
import OrderLogistics from "@/views/member/order/components/order-logistics.vue";

export default {
  name: "MemberOrder",
  components: {OrderLogistics, OrderCancel, XtxPagination, OrderItem},
  setup() {
    const activeName = ref('all')
    const clickTab = (tab) => {
      requestParams.orderState = tab.index
      requestParams.page = 1
    }

    const requestParams = reactive({
      page: 1,
      pageSize: 5,
      orderState: 0
    })

    const orderList = ref([])
    const total = ref(0)
    const loading = ref(true)

    // 使用侦听器，监听requestParams变化
    const findOrderListener = () => {
      loading.value = true
      findOrderList(requestParams).then(data=>{
        orderList.value = data.result.items
        total.value = data.result.counts
        loading.value = false
      })
    }

    watch(requestParams, () => {
      findOrderListener()
    }, {immediate:true})

    // 删除订单
    const onDeleteOrder = (item) => {
      Confirm({text:'您确认删除该条订单吗？'}).then(()=>{
        deleteOrder(item.id).then(()=>{
          message({text:'删除订单成功', type:'success'})
          findOrderListener()
        })
        // eslint-disable-next-line no-unused-vars
      }).catch(e=>{})
    }

    // 确认收货
    const useConfirmOrder = () => {
      const onConfirmOrder = (item) => {
      //   item就是你要确认收货的订单
        Confirm({text:'您确认收到货吗？确认后货款将打给卖家'}).then(()=> {
          confirmOrder(item.id).then(()=>{
            message({text:'确认收货成功', type:'success'})
            item.orderState = 4
          })
        })
      }

      return {onConfirmOrder}
    }

    // 取消订单
    const orderCancelCom = ref(null)
    const useCancelOrder = () => {
      const onCancelOrder = (item) => {
        orderCancelCom.value.open(item)
      }

      return {onCancelOrder, orderCancelCom}
    }

    // 查看物流
    const useLogisticsOrder = () => {
      const logisticsOrderCom = ref(null)
      const onLogisticsOrder = (item) => {
        logisticsOrderCom.value.open(item)
      }

      return {onLogisticsOrder,logisticsOrderCom}
    }


    return {
      activeName,
      requestParams,
      orderCancelCom,
      useCancelOrder,
      onDeleteOrder,
      loading,
      total,
      orderStatus,
      clickTab,
      orderList,
      ...useCancelOrder(),
      ...useConfirmOrder(),
      ...useLogisticsOrder()
    }
  }
}

export const useLogisticsOrder = () => {
  const orderLogisticsCom = ref(null)
  const onLogisticsOrder = (order) => {
    orderLogisticsCom.value.open(order)
  }

  return {
    onLogisticsOrder,
    orderLogisticsCom
  }
}

// 取消订单
export const useCancelOrder = () => {
  const orderCancelCom = ref(null)
  const onOrderCancel = (order) => {
    orderCancelCom.value.open(order)
  }

  return {
    orderCancelCom,
    onOrderCancel
  }
}

// 确认收货
export const useConfirmOrder = () => {
  const onOrderConfirm = (order) => {
    Confirm({text:'亲，您确认收货吗？确认后货款将打给卖家。'}).then(()=>{
      confirmOrder(order.id).then(()=>{
        message({type:'success', text:'确认收货成功'})
        order.orderState = 4
      })
    }).catch()
  }

  return {onOrderConfirm}
}
</script>

<template>
  <div class="member-order-page">
    <XtxTabs v-model="activeName" @click-tab="clickTab">
      <XtxTabsPanel
          v-for="item in orderStatus"
          :key="item.name"
          :name="item.name"
          :label="item.label"
      >{{ item.label }}
      </XtxTabsPanel>
    </XtxTabs>
    <div class="order-list">
      <div class="loading" v-if="loading"></div>
      <div class="none" v-if="!loading && orderList.length === 0">暂无数据</div>
      <OrderItem
          v-for="item in orderList"
          :key="item.id"
          :order="item"
          @on-cancel-order="useCancelOrder().onCancelOrder(item)"
          @on-delete-order="onDeleteOrder(item)"
          @on-confirm-order="onConfirmOrder(item)"
      />
    </div>
    <XtxPagination
        v-if="total > requestParams.pageSize"
        @current-change="(newPage) =>{requestParams.page = newPage}"
        :total="total"
        :pageSize="requestParams.pageSize"
        :myCurrPage="requestParams.page"
        />
    <Teleport to="#dailog">
      <OrderCancel ref="orderCancelCom"/>
    </Teleport>
    <Teleport to="#dailog">
      <OrderLogistics ref="logisticsOrderCom"/>
    </Teleport>
  </div>
</template>

<style scoped lang="less">
.order-list {
  padding: 20px;
}

.loading {
  height: 100%;
  width: 100%;
  position: absolute;
  left: 0;
  top: 0;
  background: rgba(255, 255, 255, .9) url(../../../assets/images/loading.gif) no-repeat center;
}

.none {
  height: 400px;
  text-align: center;
  line-height: 400px;
  color: #999;
}
</style>